package com.lmabbe.common.datasources.handle;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.lmabbe.common.global.context.TenantBuildContext;
import com.lmabbe.common.global.context.metadata.client.OauthClient;
import com.lmabbe.common.global.context.metadata.dict.SystemDict;
import com.lmabbe.common.global.context.metadata.param.SystemParam;
import com.lmabbe.common.global.context.metadata.tenant.Database;
import com.lmabbe.common.global.context.metadata.tenant.MiniApp;
import com.lmabbe.common.global.context.metadata.tenant.TenantInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * 租户初始化处理器
 *
 * @author lmabbe
 */
@Slf4j
public class TenantInitHandle {

    public static List<TenantBuildContext> contexts = new ArrayList<>();
    private final ApplicationContext context;

    public TenantInitHandle(ApplicationContext context) {
        this.context = context;
    }

    /**
     * 构建租户上下文
     *
     * @return {@link List< TenantBuildContext>}
     * @date 2021/12/25 5:57 PM
     * @author liming
     */
    public List<TenantBuildContext> buildContext() {
        DynamicRoutingDataSource dynamicRoutingDataSource = (DynamicRoutingDataSource) context.getBean(DataSource.class);
        DataSource aDefault = dynamicRoutingDataSource.getDataSource("default");
        try (Connection connection = aDefault.getConnection();
             Statement statement = connection.createStatement()) {
            ResultSet resultSet = statement.executeQuery("select * from tenant_install_context");
            while (resultSet.next()) {
                contexts.add(readTenantBuildContext(resultSet));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return contexts;
    }

    /**
     * 读取ResultSet
     *
     * @param resultSet
     * @return
     * @throws SQLException
     */
    private TenantBuildContext readTenantBuildContext(ResultSet resultSet) throws SQLException {
        return new TenantBuildContext()
                .setTenantId(resultSet.getLong("tenant_id"))
                .setTenantInfo(JSONObject.parseObject(resultSet.getString("tenant_info"), TenantInfo.class))
                .setTenantDatabases(JSONArray.parseArray(resultSet.getString("tenant_databases"), Database.class))
                .setSystemParams(JSONArray.parseArray(resultSet.getString("sys_params"), SystemParam.class))
                .setSysDictionaries(JSONArray.parseArray(resultSet.getString("sys_dices"), SystemDict.class))
                .setClients(JSONArray.parseArray(resultSet.getString("clients"), OauthClient.class))
                .setTenantMiniapps(JSONArray.parseArray(resultSet.getString("tenant_miniapps"), MiniApp.class))
                .setVersion(resultSet.getString("version"));
    }

}
